package cn.org.dian.easycommunicate.util;

import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.os.Process;
import android.util.Log;


public class NetworkTrafficStats {
    
    private static boolean isCounting = false;
    
    private static int myUid;
    
    private static long beginRxBytes;
    
    private static long beginTxBytes;
    
    private static final String TAG = "NetworkTrafficStats";
    
    private static boolean availbility = false;
    
    static {
        myUid = Process.myUid();
        // check availbility
        if (TrafficStats.getMobileRxBytes() == TrafficStats.UNSUPPORTED) {
            availbility = false;
        } else {
            availbility = true;
        }
    }

    /**
     * call before network request
     * @param isGlobal true if count global traffic stats
     */
    public static void beginTrafficStats(boolean isGlobal) {

        if (!availbility) {
            return;
        }
        NetworkInfo networkInfo = Utilities.getConnectivityManager().getActiveNetworkInfo();
        
        if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
            int type = networkInfo.getType();
            if (type == ConnectivityManager.TYPE_MOBILE) { //TYPE_MOBILE 0  TYPE_WIFI 1
                isCounting = true;
                if (isGlobal) {
                    beginRxBytes = TrafficStats.getMobileRxBytes();
                    beginTxBytes = TrafficStats.getMobileTxBytes();
                } else {
                    beginRxBytes = TrafficStats.getUidRxBytes(myUid);
                    beginTxBytes = TrafficStats.getUidTxBytes(myUid);
                }

            } else {
                isCounting = false; // does not count for wifi network
            }
        } else {
            isCounting = false;
        }
    }
    
    /**
     * call after network request
     * @param isGlobal true if count global traffic stats
     */
    public static void endTrafficStats(boolean isGlobal) {
        if (!availbility) {
            return;
        }
        if (isCounting) {
            isCounting = false;
            long endRxBytes;
            long endTxBytes;
            if (isGlobal) {
                endRxBytes = TrafficStats.getMobileRxBytes();
                endTxBytes = TrafficStats.getMobileTxBytes();
            } else {
                endRxBytes = TrafficStats.getUidRxBytes(myUid);
                endTxBytes = TrafficStats.getUidTxBytes(myUid);
            }
            
            long totalRxTxBytes = endRxBytes - beginRxBytes + endTxBytes - beginTxBytes;
            Log.d(TAG, "Traffic stats in bytes: " + totalRxTxBytes);
            // get the original stats from shared pref
            SharedPreferences sp = Utilities.getSharedPreferences();
            long originalStats = sp.getLong(Constants.PREF_KEY_MOBILE_NETWORK_STATS, 0);
            
            // put the new stats into shared pref
            sp.edit().putLong(
                    Constants.PREF_KEY_MOBILE_NETWORK_STATS, originalStats + totalRxTxBytes)
                    .commit(); 
        }
    }

    public static boolean getAvailbility() {
        return availbility;
    }
}
